import XEUtils from "xe-utils";
import VXETable from "vxe-table";
import utils from "@common/utils/utils";
import common from './common';
import CustomSelect from "../components/CustomSelect";
import VxeCellLabel from "../components/VxeCellLabel";

VXETable.renderer.add("CustomSelect", {
    autofocus: "input.el-input__inner",
    renderEdit(h, renderOpts, params) {
        const { row, column } = params;
        const { attrs, props } = renderOpts;
        let cellValue = XEUtils.get(row, column.property);
        return [
            h(CustomSelect, {
                attrs: attrs,
                props: $.extend({}, props, { params, value: cellValue }),
                on: common.getEditOns(renderOpts, params),
                nativeOn: common.getNativeOns(renderOpts, params)
            })
        ];
    },
    // 可编辑显示模板
    renderCell (h, renderOpts, params) {
        const {row, column} = params;
        const { formatter } = renderOpts;
        const { options, dataMethod } = renderOpts.props;
        let cellValue = XEUtils.get(row, column.property);
        if (typeof formatter === 'function') {
            cellValue = formatter(row, column);
        } else if (typeof column.params.formatter === 'function') {
            cellValue = column.params.formatter(params);
        } else {
            let dataList = options || [];
            if (typeof dataMethod === 'function') {
                dataList = dataMethod(row, column);
            }
            let filter = dataList.filter(item => item.value === cellValue);
            if (utils.isNotEmptyList(filter)) {
                cellValue = filter[0].label;
            }
        }
        return [h(VxeCellLabel, {
            props: {
                node: cellValue,
            },
        })];
    }
});
